//
//  MeMessageViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/19.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeMessageViewController.h"
#import "MeMessageViewCell.h"
#import "dataNews.h"
#import "taskHomePageService.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import <Masonry.h>
#import "MeMessageDetailViewController.h"

@interface MeMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,strong)NSArray        *dataArray;
@end

@implementation MeMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息中心";
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self tableView];
    [self loadNews];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
}

-(void)loadNews{
    self.dataArray = [NSArray new];
    [DKProgressHUD showLoading];
    [taskHomePageService getNews:nil success:^(dataNews *profile, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            if (profile.data) {
                self.dataArray = profile.data;
                [self.tableView reloadData];
            }
        }else{
            [DKProgressHUD showErrorWithStatus:@"加载失败"];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.view).offset(distanceHeight);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor whiteColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [_tableView registerClass:[MeMessageViewCell class] forCellReuseIdentifier:@"MeMessageViewCell"];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    dataNewsInfo *info = [self.dataArray objectAtIndex:indexPath.row];
    id model = [dataNewsInfo mj_objectWithKeyValues:info];
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[MeMessageViewCell class] contentViewWidth:KScreenWidth];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeMessageViewCell"];
    if (self.dataArray.count > 0) {
        dataNewsInfo *info = [self.dataArray objectAtIndex:indexPath.row];
        dataNewsInfo *infoModel = [dataNewsInfo mj_objectWithKeyValues:info];
        [cell setModel:infoModel];
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count > 0) {
        dataNewsInfo *info = [self.dataArray objectAtIndex:indexPath.row];
        dataNewsInfo *infoModel = [dataNewsInfo mj_objectWithKeyValues:info];
        MeMessageDetailViewController *detail = [MeMessageDetailViewController new];
        detail.model = infoModel;
        [self.navigationController pushViewController:detail animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
